/*
 * Copyright (c) 2008 VMware, Inc.
 * Copyright (c) 2009 John Pritchard, WTKX Project Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package wtkx.in;

import wtkx.Application;
import wtkx.Window;
import wtkx.collections.Dictionary;
import wtkx.wtk.ApplicationContext;
import wtkx.wtk.WindowClassListener;
import wtkx.wtk.media.Image;
import wtkx.wtk.media.Picture;

/**
 * Application context used to execute applications in a native frame
 * window.
 *
 * @author gbrown
 */
@SuppressWarnings("deprecation")
public final class Desktop
    extends ApplicationContext 
{

    public static class IconInstaller 
        extends Object
        implements WindowClassListener
    {
        public static class Callback
            extends Object
            implements Runnable
        {
            private Frame frame;

            public Callback(Frame frame){
                super();
                this.frame = frame;
            }

            public void run() {
                Window activeWindow = Window.getActiveWindow();
                if (null != activeWindow) {
                    Window rootOwner = activeWindow.getRootOwner();
                    if (null != rootOwner){
                        Image rootIcon = rootOwner.getIcon();
                        if (rootIcon instanceof Picture) {
                            Picture rootPicture = (Picture)rootIcon;
                            this.frame.setIconImage(rootPicture.getBufferedImage());
                        }
                    }
                }
            }
        }

        private Frame frame;

        public IconInstaller(Frame frame){
            super();
            this.frame = frame;
        }
        public void activeWindowChanged(Window previousActiveWindow) {
            ApplicationContext.queueCallback(new Callback(this.frame));
        }
    }

    public final static void InstallIcon(Frame frame){

        Window.getWindowClassListeners().add(new IconInstaller(frame));
    }


    public Desktop(Dictionary<String,String> properties, 
                   Application application)
    {
        super(properties,application);
    }


}
